//
//  DetailQualityViewController.m
//  Beauty
//
//  Created by Snow on 16/9/10.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "DetailQualityViewController.h"
#import "GXMacro.h"
#import "GXNetWorkingParser.h"
#import "QualitySortTableViewCell.h"
#import "DetailNewProductTableViewCell.h"
#import "HotSaleTableViewCell.h"
#import "ModelOfHotSale.h"
#import "SearchViewController.h"
#import "NewProductViewController.h"
@interface DetailQualityViewController ()<UITableViewDelegate, UITableViewDataSource, QualitySortDelegate, DetailNewProductDelegate, HotScaleDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *arrayOfQuality;
@property (nonatomic, strong) NSString *gc_id;
@property (nonatomic, strong) NSMutableArray *arrayOfHotSale;
@end

@implementation DetailQualityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    [self createView];
}
- (void)handleData
{
    _arrayOfQuality = [NSMutableArray array];
    _arrayOfHotSale = [NSMutableArray array];
    if (_gc_id.length != 0) {
        NSString *url = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_list&gc_id=%@&key=1&page=10", _gc_id];
        NSString *urlOfSale = [NSString stringWithFormat:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_list&gc_id=%@&page=10", _gc_id];

        [GXNetWorkingParser GETWithURL:url par:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject[@"datas"];
            _arrayOfQuality = dic[@"goods_list"];
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
        [GXNetWorkingParser GETWithURL:urlOfSale par:nil success:^(id responseObject) {
            NSDictionary *dic = responseObject[@"datas"];
            NSArray *arr = dic[@"goods_list"];
            for (NSDictionary *dict in arr) {
                ModelOfHotSale *model = [[ModelOfHotSale alloc] init];
                [model setValuesForKeysWithDictionary:dict];
                [_arrayOfHotSale addObject:model];
            }
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
    } else {
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_list&gc_id=1543@&key=1&page=10" par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        _arrayOfQuality = dic[@"goods_list"];
        [_tableView reloadData];
    } filed:^(NSError *error) {
        
    }];
        
    [GXNetWorkingParser GETWithURL:@"http://www.wantease.com/mobile1/index.php?act=goods&op=goods_list&gc_id=1543@&page=10" par:nil success:^(id responseObject) {
        NSDictionary *dic = responseObject[@"datas"];
        NSArray *arr = dic[@"goods_list"];
        for (NSDictionary *dict in arr) {
            ModelOfHotSale *model = [[ModelOfHotSale alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arrayOfHotSale addObject:model];
        }
        [_tableView reloadData];
    } filed:^(NSError *error) {
            
        }];
    }
}
- (void)createView
{
    //搜索栏
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(5, 5, SCREEN_WIDTH - 10, 50)];
    view.backgroundColor = [UIColor whiteColor];
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(view.frame.size.width / 2 - 30, 10, 20, 20)];
    image.image = [UIImage imageNamed:@"search"];
    [view addSubview:image];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width / 2, 5, 80, 30)];
    label.text = @"搜索";
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
    [view addGestureRecognizer:tap];
    [view addSubview:label];

    [self.view addSubview:view];

    //tableView
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50 - 64) style:UITableViewStyleGrouped];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[QualitySortTableViewCell class] forCellReuseIdentifier:@"QualitySortTableViewCell"];
    [_tableView registerClass:[HotSaleTableViewCell class] forCellReuseIdentifier:@"HotSaleTableViewCell"];
    [_tableView registerClass:[DetailNewProductTableViewCell class] forCellReuseIdentifier:@"DetailNewProductTableViewCell1"];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        QualitySortTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QualitySortTableViewCell"];
        cell.delegate = self;
        return cell;
    } else if (indexPath.section == 1) {
        DetailNewProductTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailNewProductTableViewCell1"];
        cell.delegate = self;
        if (_arrayOfQuality.count != 0) {
            cell.array = _arrayOfQuality;
        }
        return cell;
    } else {
        HotSaleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HotSaleTableViewCell"];
        cell.delegate = self;
        if (_arrayOfHotSale.count != 0) {
            cell.arrayOfModel = _arrayOfHotSale;
        }
        return cell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 100;
    } else if(indexPath.section == 1){
        return 180;
    } else {
        NSInteger i = ceilf(_arrayOfHotSale.count / 2);
        return 300 * i;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark ------------  去掉tableView的头部分区悬浮效果  -----------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    CGFloat sectionHeaderHeight = 10;
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-offsetY, 0, 0, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= scrollView.contentSize.height - scrollView.frame.size.height)
    {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}
- (void)sendGc_Id:(NSString *)gc_id
{
    _gc_id = gc_id;
    [self handleData];
    [_tableView reloadData];
}


//搜索的手势
- (void)handleTap:(UITapGestureRecognizer *)tap
{
    SearchViewController *vcOfResult = [[SearchViewController alloc]init];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = barItem;
    self.navigationItem.backBarButtonItem.tintColor = [UIColor whiteColor];
    vcOfResult.string = self.string;
    [self.navigationController pushViewController:vcOfResult animated:YES];
}
- (void)getGoodDetailWithGoodId:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)hotScaleWithGood_ID:(NSString *)good_id
{
    NewProductViewController *new = [[NewProductViewController alloc] init];
    new.goods_id = good_id;
    [self.navigationController pushViewController:new animated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
